﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.UI.Web.Shop.Data;
using Medianamik.UI.Web.Shop.Data.Repositories;

namespace Medianamik.UI.Web.Shop.Presentation.Presenters
{
    public class ProductDistinctListPresenter<TProduct, TCategory, TBrand, TProductsInCategories, TView> :
        ProductListPresenter<TProduct, TCategory, TBrand, TProductsInCategories, TView>
        where TProduct : class, Data.Interfaces.IProduct
        where TCategory : class, Data.Interfaces.ICategory
        where TBrand : class, Data.Interfaces.IBrand
        where TProductsInCategories : class, Data.Interfaces.IProductsInCategories
        where TView : class, IProducts
    {
        public ProductDistinctListPresenter(TView view, IProductRepository<TProduct> productRepository, INodeTypeRepository nodeTypeRepository, IMedianamikRepository<IMedianamikDTOWithName> baseTypeRepository, ICategoryRepository<TCategory> categoriesRepository, IBrandRepository<TBrand> brandsRepository, IProductsInCategoriesRepository<TProductsInCategories> productsInCategoriesRepository)
            : base(view, productRepository, nodeTypeRepository, baseTypeRepository, categoriesRepository, brandsRepository, productsInCategoriesRepository)
        {
        }

        protected override PagedCollection<TProduct> GetData()
        {
            var keywords = String.IsNullOrEmpty(View.Keywords)
               ? null
               : String.Concat("%", View.Keywords, "%");

            var data = Repository.PagedWithFilters(CurrentEditionCulture, ContentState.Draft, (ActiveStatus)Status, PageIndex, PageSize, View.ModuleNodeId, null, SelectedBrand,
                                                   SelectedCategory, keywords);
            return data;
        }

        protected override void SetCategoryName(ITextControl categoryNameControl, TProduct element)
        {
            var categoryName = "";

            if (ProductsInCategoriesList.Count > 0)
            {
                var selectedProductsInCategories = ProductsInCategoriesList
                    .Where(p => p.Products == element.NodeId).ToList();

                for (var i = 0; i < selectedProductsInCategories.Count; i++)
                {
                    var category = Categories.Where(cat => cat.NodeId == selectedProductsInCategories.ElementAt(i).Categories).DefaultIfEmpty(null).First();

                    if (category != null)
                    {
                        var level = 0;
                        var categories = Categories;//Pour avoir avec le setter
                        categoryName += CategoriesRepository.GetCategoryNameWithParent(
                            category.ParentNodeId.Value, ref level, ref categories) + category.Name;
                    }

                    if (i != selectedProductsInCategories.Count - 1)
                    {
                        categoryName += "<br />";//"\r\n";
                    }
                }
            }

            categoryNameControl.Text = categoryName;
        }

        class ProductComparer<TProduct> : IEqualityComparer<TProduct>
            where TProduct : class, Data.Interfaces.IProduct
        {
            public bool Equals(TProduct x, TProduct y)
            {
                return x.NodeId == y.NodeId;
            }

            public int GetHashCode(TProduct obj)
            {
                return obj.NodeId.GetHashCode();
            }
        }
    }
}
